import React, {Component} from 'react';
import PropTypes from 'prop-types'
import './index.css'
import {nanoid} from "nanoid";

class Header extends Component {
    static propTypes={
        addTodo:PropTypes.func.isRequired
    }
    addTodo = (e) => {
        const {keyCode, target} = e
        if (keyCode === 13) {
            if (target.value.trim() === '') {
                alert('输入不能为空')
                return
            }
            const todo = {id: nanoid(), name: target.value.trim(), checked: false}
            this.props.addTodo(todo)
            target.value=''
        }
    }

    render() {
        return (
            <div className='todo-header'>
                <input type="text" placeholder="请输入你的任务名称，按回车键确认" onKeyUp={this.addTodo}/>
            </div>
        );
    }
}

export default Header;